package com.console.operations.planmgr;

import com.common.test.OperationBase;
import com.common.test.UITestBase;
import com.common.util.DBUtil;
import com.console.pages.planmgr.FinancePlanAuditPage;

public class FinancePlanAudit extends OperationBase {

	public FinancePlanAudit(UITestBase tb){
		super(tb);
	}
	
	/*
	 * 审核后的理财计划状态是否正确
	 */
	public boolean isplanstatusafterauditcorrect = false;
	
	public void audit(String planname, String isfrontdisplay) throws Exception{
		switchToDefaultContent();
		FinancePlanAuditPage fpap = new FinancePlanAuditPage(tb);
		elementToBeClickable(fpap.panelhead);
		waitForElementDisplayed(fpap.panelhead);
		waitForElementDisplayed(fpap.lastpageLink);
		pause(2000);
		int pagecount = Integer.parseInt(fpap.lastpageLink.getText());
		log.info("债权审核页的页数为 " + pagecount);
		//设置一个是否找到债权的标志位
		boolean isfound = false;
		//定位对应理财计划审核链接的setfindName
		fpap.auditLink.setfindName("xpath");
		//定位对应理财计划审核链接的findValue
		fpap.auditLink.setfindValue("//tr[td[text()='"+ planname + "']]/td[last()]/a");
		//分页查找是有有对应理财计划的
		for(int i=1;i<=pagecount;i++){
			fpap.presentpageLinkli.setfindName("xpath");
			fpap.presentpageLinkli.setfindValue("//ul[@class='pagination']/li[a[text()='" + i + "']]");
			fpap.presentpageLink.setfindName("linkText");
			fpap.presentpageLink.setfindValue(String.valueOf(i));
			waitForElementExist(fpap.presentpageLink);
			fpap.presentpageLink.click();
			log.info("点击第" + i + "页链接");
			waitForPageLinkclicked(fpap.presentpageLinkli);
			if(isPresent(fpap.auditLink)){
				isfound = true;
				break;
			}
		}
		if(!isfound){
			handlefailure("页面上找不到该理财计划");
		}
		pause(600);
		waitForElementExist(fpap.auditLink);
		fpap.auditLink.click();
		waitForPageContainsStr("审核【" + planname);
		fpap.passbutton.click();
		waitForElementDisplayed(fpap.passsubmitbutton);
		fpap.passsubmitbutton.click();
		waitForPageContainsStr("已通过");
		pause(800);
		String planstatus = DBUtil.queryOneCell("select dc_status\n"
											  + "from fiz_plan\n"
											  + "where vc_name = ?", new Object[]{planname});
		if(isfrontdisplay.equals("是")){
			isplanstatusafterauditcorrect = planstatus.equals("01");
		}else if(isfrontdisplay.equals("否")){
			isplanstatusafterauditcorrect = planstatus.equals("02");
		}else{
			throw new Exception("没有这种显示类型");
		}
	}
}
